Geographic Services
This section documents the geographic services available in the codebase, including mapping, geocoding, and location-based services.
Table of Contents
Google Maps Tools
Building and Property Analysis
Files:
models/geotools/maps.pymodels/geotools/maps2.pymodels/geotools/maps2.1.pymodels/geotools/maps3.pymodels/geotools/maps4.py
These modules provide tools for analyzing satellite imagery and identifying building properties using the Google Maps API and computer vision techniques.
Basic Satellite Image Analysis
from models.geotools.maps import fetch_satellite_image, preprocess_image, detect_buildings
# Fetch satellite image
lat, lon = 37.3246985, -121.924469 # San Jose, CA
image_path = fetch_satellite_image(lat, lon, zoom=20, size="640x640", map_type="satellite")
# Preprocess the image for analysis
image, edges = preprocess_image(image_path)
# Detect buildings in the image
contours = detect_buildings(edges)
# Filter for rectangular buildings
from models.geotools.maps import filter_rectangular_contours
rectangles = filter_rectangular_contours(contours, min_area_threshold=100)
# Calculate areas and bounding boxes
from models.geotools.maps import calculate_area_and_bbox
total_area, bounding_boxes = calculate_area_and_bbox(rectangles, pixel_resolution=0.3)
print(f"Estimated Building Area: {total_area:.2f} square meters")
# Visualize and save results
from models.geotools.maps import visualize_and_save
result_path = visualize_and_save(image, rectangles)
Advanced Building Outline Detection
from models.geotools.maps2_1 import fetch_satellite_images, add_building_marker_and_outline
# Fetch high-quality satellite images at multiple resolutions
lat, lon = 37.3385655, -121.9301624 # San Jose, CA
zoom_level = 19
image_paths = fetch_satellite_images(lat, lon, zoom=zoom_level, map_type="satellite")
# Add marker, outline building, calculate area and add scale bar
for size_label, image_path in image_paths.items():
processed_path = f"satellite_with_outline_{size_label}.png"
result_path, building_area = add_building_marker_and_outline(
image_path, processed_path, lat, zoom_level
)
print(f"{size_label.capitalize()} image: {result_path}")
print(f"Building area: {building_area:.2f} square meters")
Google Maps Building Data API
from models.geotools.maps3 import get_building_data_from_coordinates
# Get building data from coordinates
lat, lng = 37.3385655, -121.9301624
building_data = get_building_data_from_coordinates(lat, lng)
# Get building outlines and entrances
from models.geotools.maps3 import get_satellite_image, plot_building_data
# Get satellite image
satellite_image = get_satellite_image(lat, lng, zoom=20, size="600x600")
# Plot building outlines and entrances on the image
plot_building_data(satellite_image, building_data, lat, lng)
Building Extraction API
# Using the building extraction API (Flask-based)
import requests
import json
# API endpoint (when running locally)
endpoint = "http://localhost:5000/extract_buildings"
# Send coordinates for building extraction
data = {
"latitude": 37.3385655,
"longitude": -121.9301624
}
response = requests.post(endpoint, json=data)
result = response.json()
# Get the URL to the processed image with building outlines
image_url = result["image_url"]
# Get GeoJSON polygons of the detected buildings
polygons = result["polygons"]
OpenStreetMap Integration
Files:
models/geotools/osm/maps1.pymodels/geotools/osm/maps2.pymodels/geotools/osm/maps3.pymodels/geotools/osm/maps4.py
These modules provide tools for interacting with OpenStreetMap data to extract building information.
Basic OpenStreetMap Building Data
from models.geotools.osm.maps1 import get_buildings_with_geometry, compute_dimensions
# Get building data within a radius
lat = 37.3421214
lon = -121.8880772
radius = 50 # meters
building_data = get_buildings_with_geometry(lat, lon, radius)
# Analyze buildings
elements = building_data.get("elements", [])
for element in elements:
elem_id = element.get("id")
elem_type = element.get("type")
tags = element.get("tags", {})
geometry = element.get("geometry", [])
# Compute dimensions
dimensions = compute_dimensions(geometry)
print(f"Element ID: {elem_id}, Type: {elem_type}")
print(f"Width (m): {dimensions['width_m']:.2f}")
print(f"Height (m): {dimensions['height_m']:.2f}")
print(f"Bounding Box: {dimensions['min_lat']} to {dimensions['max_lat']}, "
f"{dimensions['min_lon']} to {dimensions['max_lon']}")
Advanced Building Analysis with Mapbox
from models.geotools.osm.maps2 import compute_dimensions, compute_zoom_level, generate_satellite_